package com.kunya.product.service.impl.product;

//import com.codingapi.tx.annotation.TxTransaction;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.kunya.common.common.Const;
import com.kunya.common.common.ServerResponse;
import com.kunya.product.dao.comment.CommentMapper;
import com.kunya.product.po.Comment;
import com.kunya.product.service.product.IProductCommentService;
import com.kunya.product.vo.CommentListVo;
import com.kunya.product.vo.CommentModel;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author zp
 * @Description: 商品评论service实现类
 * @date 2018/6/14 16:58
 */
@Service("iProductCommentService")
public class ProductCommentServiceImpl implements IProductCommentService {

    @Autowired
    private CommentMapper commentMapper;


    @Autowired
    private RestTemplate restTemplate;


    /**
     * @author zp
     * @Description: 根据商品id  分页查询商品评价列表 评价回复
     * @param
     * @date 2018/6/14 17:22
     */
    public ServerResponse<PageInfo> getCommentsByProductId( Long productId,int pageNum,int pageSize,String tablePrefix){
        PageHelper.startPage(pageNum,pageSize);
        List<Comment> commentList=commentMapper.getCommentsByProductId(productId,tablePrefix);
        if(CollectionUtils.isEmpty(commentList)){
            return ServerResponse.createByErrorMessage("评论列表为空");
        }
        List<CommentListVo> commentListVoList= Lists.newArrayList();
        for (Comment comment:commentList){
            CommentListVo commentListVo=assembleCommentListVo(comment);
            commentListVoList.add(commentListVo);
        }
      PageInfo pageInfo=new PageInfo(commentListVoList);
      return ServerResponse.createBySuccess(pageInfo);
    }
    /**
     * @author zp
     * @Description: 新增评论 ，批量
     * @param
     * @date 2018/7/16 18:00
     */
    @Transactional
//    @TxTransaction(isStart = true)
    public ServerResponse addComment(Long orderId, CommentModel commentModel,String tablePrefix){
       //1.判断订单状态是否为待评价
        //根据订单编号获取订单状态 调用order服务
          Byte orderStatus=(byte)restTemplate.getForObject("http://order/order/searchOrderStatus?orderId="+orderId+"&tablePrefix="+tablePrefix,ServerResponse.class).getData();
          if (orderStatus!= Const.OrderStatusEnum.SHIPPED.getCode()){
             return ServerResponse.createByErrorMessage("订单状态错误");
          }
        //2.循环评论list ，将评论插入，如果list中有空对象，将对象设为默认值
        List<Comment> commentList=commentModel.getComments();
          for (Comment comment:commentList){
              if(StringUtils.isEmpty(comment.getContent())){
                  //默认好评
                  comment.setContent("系统默认好评");
              }
              if (comment.getStar()==null){
                  //默认五星
                  comment.setStar((byte)5);
              }
          }
          int resultCount=commentMapper.batchInsertComment(commentList,tablePrefix);
          if(resultCount<=0){
              return ServerResponse.createByErrorMessage("评论失败");
          }
          //将commentList插入数据库
        // 3.更新订单状态
        //调用订单服务更新订单状态
           boolean result=false;
            result =restTemplate.getForObject("http://order/order/updateOrderStatus?orderId="+orderId+"&tablePrefix="+tablePrefix,ServerResponse.class).isSuccess();
          //如果被调用接口业务执行失败，手动抛出异常，回滚
          if(false==result){
              throw new RuntimeException();
          }
        return  ServerResponse.createBySuccess();
    }
    @Transactional
//    @TxTransaction(isStart = true)
    public ServerResponse test(){
        String tablePrefix="340_";
        ServerResponse serverResponse=restTemplate.getForObject("http://order/order/updateOrderStatus?orderId=72&status=8&tablePrefix="+tablePrefix,ServerResponse.class);
        int a=100/0;
        return  serverResponse;
    }

    /**
     * @author zp
     * @Description: 将Comment po转为CommentVo
     * @param
     * @date 2018/6/15 17:05
     */
    private CommentListVo  assembleCommentListVo(Comment comment){
        CommentListVo commentListVo=new CommentListVo();
        BeanUtils.copyProperties(comment,commentListVo);
        return commentListVo;
    }

}
